package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
	"wikisaver/config"
	"wikisaver/models"
)

func main() {
	conf := config.Conf
	f, err := os.Open(conf.WikiPath)
	if err != nil {
		fmt.Println("open file error")
	}
	defer f.Close()

	lineChan := make(chan string, 10)
	workerCount := conf.WorkerCount
	for i:=0; i<workerCount; i++ {
		go func() {
			for {
				add := <- models.SaveChan
				add.Add()
			}
		}()
		go func() {
			for {
				line := <-lineChan
				parseline(line)
			}
		}()
	}

	reader := bufio.NewReader(f)
	x := 0
	for line, err := reader.ReadString('\n'); err == nil; line, err = reader.ReadString('\n') { 
		x++
		if (len(line) < 3) {
			continue;
		}
		if conf.Lines>0 && x > conf.Lines + 1 {
			break
		}
		lineChan <- strings.TrimSpace(line)
	}
	select{}
}

func UnescapeUnicode(raw string) (string, error) {
	str, err := strconv.Unquote(strings.Replace(strconv.Quote(raw), `\\u`, `\u`, -1))
	if err != nil {
		return "", err
	}
	return str, nil
}
